﻿using System;
using System.Web;
using System.Text;
using MySql.Data.MySqlClient;
using System.Configuration;

namespace Com.Toook.Common
{
    /// <summary>
    /// MySql辅助类
    /// </summary>
    public class MySqlHelperMy
    {
        /// <summary>
        /// MySql数据库默认连接字符串
        /// </summary>
        private static string connectionString = ConfigurationManager.ConnectionStrings["MySqlConnectionString"].ConnectionString;
        
        static MySqlHelperMy()
        {
        }
        /// <summary>
        /// 获取MySql数据库连接对象, 默认devDB.db
        /// </summary>
        /// <returns></returns>
        public static MySqlConnection GetConnection()
        {
            MySqlConnection con = new MySqlConnection(connectionString);
            return con;
        }

        /// <summary>
        /// 关闭数据库资源
        /// </summary>
        /// <param name="con">数据库连接，没有传null</param>
        /// <param name="reader">MySqlDataReader，没有传null</param>
        public static void CloseDbResource(MySqlConnection con, MySqlDataReader reader)
        {
            if(reader != null)
            {
                try
                {
                    reader.Close();
                }
                catch (Exception ex)
                {
                    LogHelper.Log.Error("MySqlHelper.CloseDbResource reader 错误", ex);
                }
            }
            if(con != null)
            {
                try
                {
                    con.Close();
                }
                catch (Exception ex)
                {
                    LogHelper.Log.Error("MySqlHelper.CloseDbResource con 错误", ex);
                }
            }
        }       
       
    }
}
